package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.ITransferAccountsZfbDetailDAO;
import org.jsola.hr.dto.TransferAccountsZfbDetailAddDTO;
import org.jsola.hr.dto.TransferAccountsZfbDetailUpdateDTO;
import org.jsola.hr.entity.TransferAccountsZfbDetailDO;
import org.jsola.hr.query.TransferAccountsZfbDetailQuery;
import org.jsola.hr.service.ITransferAccountsZfbDetailService;
import org.jsola.hr.vo.TransferAccountsZfbDetailListVO;
import org.jsola.hr.vo.TransferAccountsZfbDetailVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 转账明细(支付宝)
 *
 * @author zhr
 */
@Service("hrTransferAccountsZfbDetailServiceImpl")
public class TransferAccountsZfbDetailServiceImpl implements ITransferAccountsZfbDetailService {

    @Autowired
    private ITransferAccountsZfbDetailDAO transferAccountsZfbDetailDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public TransferAccountsZfbDetailVO save(TransferAccountsZfbDetailAddDTO transferAccountsZfbDetailAddDTO, TokenUser tokenUser) {
        // 转DO
        TransferAccountsZfbDetailDO transferAccountsZfbDetailDO = transferAccountsZfbDetailAddDTO.to(TransferAccountsZfbDetailDO.class);
        // 保存
        transferAccountsZfbDetailDO = save(transferAccountsZfbDetailDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return transferAccountsZfbDetailDO.to(TransferAccountsZfbDetailVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(TransferAccountsZfbDetailUpdateDTO transferAccountsZfbDetailUpdateDTO, TokenUser tokenUser) {
        //转DO
        TransferAccountsZfbDetailDO transferAccountsZfbDetailDO = transferAccountsZfbDetailUpdateDTO.to(TransferAccountsZfbDetailDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(transferAccountsZfbDetailDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... transferAccountsZfbDetailIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) transferAccountsZfbDetailIds);
    }


    @Override
    public TransferAccountsZfbDetailVO selectById(String transferAccountsZfbDetailId, String siteId) {
        TransferAccountsZfbDetailDO transferAccountsZfbDetailDO = selectDOById(transferAccountsZfbDetailId, siteId);
        if (transferAccountsZfbDetailDO == null) {
            return null;
        }
        return transferAccountsZfbDetailDO.to(TransferAccountsZfbDetailVO.class);
    }

    @Override
    public List<TransferAccountsZfbDetailListVO> select(TransferAccountsZfbDetailQuery transferAccountsZfbDetailQuery, String siteId) {
        List<TransferAccountsZfbDetailDO> transferAccountsZfbDetailDOList = selectDO(transferAccountsZfbDetailQuery, siteId);
        if(CollectionUtils.isEmpty(transferAccountsZfbDetailDOList)) {
            return transferAccountsZfbDetailDOList == null ? null : new ArrayList<>();
        }
        return transferAccountsZfbDetailDOList.stream()
                .map(transferAccountsZfbDetailDO -> transferAccountsZfbDetailDO.to(TransferAccountsZfbDetailListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(TransferAccountsZfbDetailQuery transferAccountsZfbDetailQuery, String siteId) {
        Example example = buildExample(transferAccountsZfbDetailQuery,siteId);
        return transferAccountsZfbDetailDAO.selectCountByExample(example);
    }

    @Override
    public Page<TransferAccountsZfbDetailListVO> selectPage(TransferAccountsZfbDetailQuery transferAccountsZfbDetailQuery, String siteId) {
        Example example = buildExample(transferAccountsZfbDetailQuery,siteId);
        Page<TransferAccountsZfbDetailDO> page = transferAccountsZfbDetailDAO.selectPageByExample(example,
                transferAccountsZfbDetailQuery.getPageNo(),
                transferAccountsZfbDetailQuery.getPageSize());

        return page.to(TransferAccountsZfbDetailListVO.class);
    }

    @Override
    public TransferAccountsZfbDetailDO selectDOById(String transferAccountsZfbDetailId, String siteId) {
        return listById(transferAccountsZfbDetailId, siteId);
    }

    @Override
    public List<TransferAccountsZfbDetailDO> selectDO(TransferAccountsZfbDetailQuery transferAccountsZfbDetailQuery, String siteId) {
        Example example = buildExample(transferAccountsZfbDetailQuery,siteId);
        return transferAccountsZfbDetailDAO.selectByExample(example);
    }


    /**
     * 根据查询参数，构建example

     * @param transferAccountsZfbDetailQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(TransferAccountsZfbDetailQuery transferAccountsZfbDetailQuery, String siteId) {
        Example example = new Example(TransferAccountsZfbDetailDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        // 排序
        ExampleKit.setExampleOrder(example,transferAccountsZfbDetailQuery.getOrders());
        return example;
    }
}




